import { GameCore } from "./Class";

    export type Constructor<T> = new() => T
    export type Constructors<T> = new(...args : any[]) => T
    const classNameMap : Map<Function, string> = new Map<Function, string>();
    export const getClassName = function<T> (obj : Constructor<T>){ 
        var key = obj;
        
        if(classNameMap.has(key))
        {
            return classNameMap.get(key);
        }
        console.error("must call invoke setClassName first!!!! ");
        return null;
    }

    export const setClassName = function<T>(obj : Constructor<T>, className : string) 
    {
        var key = obj;
        if(classNameMap.has(key))
        {
            if(className == classNameMap.get(key))
            {
                console.error("to mush some class " + className);
            }
            return
        }
        
        classNameMap.set(key, className);
    }

    const getClass = function(className : string) : Function
    {
        classNameMap.forEach((value, key, map)=>{
            if(className == value)
            {
                return key;
            }
        });
        return null;
    }

    
    export const NewClass = function<T extends GameCore.ClassBase>(obj : Constructor<T>)
    {
        var className = getClassName(obj);
        var newObj = new obj();
        newObj.className = className;
        return newObj;
    }

    // export const NewClassByClassName = function(className : string) : object
    // {
    //     var classConstructorFunc = getClass(className);
    //     if(classConstructorFunc != null)
    //     {
    //         var classType = classConstructorFunc as Constructor<GameCore.ClassBase>;
    //         if(classType != null){
    //             var newObj = new classType();
    //             newObj.className = className;
    //             return newObj;
    //         }
    //     }

    //     console.error("can not create obj by className: " + className)
    //     return null;
    // }